package com.cdtu.labpreview.controller;

import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.common.UserHolder;
import com.cdtu.labpreview.controller.vo.PasswordVo;
import com.cdtu.labpreview.controller.vo.UserVo;
import com.cdtu.labpreview.dto.UserDTO;
import com.cdtu.labpreview.entity.*;
import com.cdtu.labpreview.mapper.StudentMapper;
import com.cdtu.labpreview.mapper.TeacherClassMapper;
import com.cdtu.labpreview.mapper.TeacherMapper;
import com.cdtu.labpreview.service.IUserService;
import com.cdtu.labpreview.util.PasswordEncoder;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 用户管理模块
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理模块")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private TeacherClassMapper teacherClassMapper;

    /**
     * 用户登录（学生、老师、管理员）
     * @param userType
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserType userType, HttpSession session){
        int loginName = userType.getLoginName();
        String password = userType.getPassword();
        int type = userType.getType();
//        session.setAttribute("type",type);
        return userService.login(loginName, password, type,session);
    }

    /**
     * 用户登出（学生、老师、管理员）
     * @return
     */
    @PostMapping("/logout")
    public Result loginOut(HttpSession session){
        UserDTO userDTO = UserHolder.getUser();
        int type = userDTO.getType();
        if(type == 1)  {
            session.removeAttribute("student");
        }else if (type == 2) {
            session.removeAttribute("teacher");
        }else {
            session.removeAttribute("admin");
        }
        return Result.ok();
    }

    /**
     * 添加用户（管理员）
     * @param userVo
     * @return
     */
    @PostMapping
    public Result addUser(@RequestBody UserVo userVo){
        int type = userVo.getType();
        User user = userVo.getUser();
        return userService.addUser(user,type);
    }

    /**
     * 绑定教师与班级（管理员）
     * @param teacherClass
     * @return
     */
    @PostMapping("/bind")
    public Result bind(@RequestBody TeacherClass teacherClass){
        teacherClassMapper.insert(teacherClass);
        return Result.ok("绑定成功！");
    }

    /**
     * 修改密码（所有角色）
     * @param passwordVo
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordVo passwordVo){
        String originPassword = passwordVo.getOriginPassword();
        String newPassword = passwordVo.getNewPassword();
        return userService.updatePassword(originPassword,newPassword);
    }

    /**
     * 重置密码(管理员)
     * @return
     */
    @GetMapping("/resetPassword")
    public Result resetPassword(Integer id){
        return userService.resetPassword(id);
    }
}
